package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.adapter.AssetDetailRecordAdapter;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.base.ConstantParam;
import com.yuwj.chinaa.entity.EntityAssetDetailRecord;
import com.yuwj.chinaa.entity.EntityPage;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 资产流水页面
 */
public class AssetDetailRocordActivity extends BaseActivity {


    IPresenter presenter;
    EntityPage entityPage;
    boolean isRefresh = true;
    int accountType;
    AssetDetailRecordAdapter assetDetailRecordAdapter;
    List<EntityAssetDetailRecord> entityAssetDetailRecordList;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.wrl_list)
    SwipeRefreshLayout wrlList;


    public static Intent createIntent(Context context, int accountType) {
        Intent intent = new Intent(context, AssetDetailRocordActivity.class);
        intent.putExtra("accountType", accountType);
        return intent;
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_asset_detail_record;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
        entityAssetDetailRecordList = new ArrayList<>();
        accountType = getIntent().getIntExtra("accountType", ConstantParam.ACCOUNT_TYPE_WITH_GTE);
        entityPage = new EntityPage();
        entityPage.setPageNo(-1);//由于首页传0,我们在发送请求时都是发送下一页,所以此处初始化为-1
        assetDetailRecordAdapter = new AssetDetailRecordAdapter(context, entityAssetDetailRecordList);
    }

    @Override
    public void initView() {
        String title = "";
        switch (accountType) {
            case ConstantParam.ACCOUNT_TYPE_WITH_GTE:
                title = "GTE明细";
                break;
            case ConstantParam.ACCOUNT_TYPE_WITH_GTK:
                title = "GTK明细";
                break;
        }
        initActionBar(toolbar, tvTitle, title);
        rvList.setLayoutManager(new LinearLayoutManager(context));
        rvList.setAdapter(assetDetailRecordAdapter);
        wrlList.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        assetDetailRecordAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, rvList);
    }

    @Override
    public void initOperator() {
        refresh();
    }

    /**
     * 根据账户类型,更新界面
     */
    public void updateAssetDetail() {
        JSONObject jsonObjWithAssetGTEDetailParam = new JSONObject();
        try {
            jsonObjWithAssetGTEDetailParam.put("page", entityPage.getComputeNextPage() + "");
            jsonObjWithAssetGTEDetailParam.put("pageSize", entityPage.getPageSize() + "");
            jsonObjWithAssetGTEDetailParam.put("sourceType", accountType + "");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.assetDetailRecord(jsonObjWithAssetGTEDetailParam.toString());
    }

    void refresh() {
        isRefresh = true;
        entityPage.setPageNo(-1);
        wrlList.setRefreshing(false);
        updateAssetDetail();
    }

    void loadMore() {
        isRefresh = false;
        updateAssetDetail();
    }


    @Override
    public void onAssetDetailRecordSuccess(List<EntityAssetDetailRecord> entityAssetDetailRecordList) {
        super.onAssetDetailRecordSuccess(entityAssetDetailRecordList);
        entityPage.setPageNo(entityPage.getComputeNextPage());
        if (entityAssetDetailRecordList != null) {
            if (entityAssetDetailRecordList.size() < entityPage.getPageSize()) {
                assetDetailRecordAdapter.loadMoreEnd();
            } else {
                assetDetailRecordAdapter.loadMoreComplete();
            }

            if (isRefresh) {
                this.entityAssetDetailRecordList.clear();
            }
            this.entityAssetDetailRecordList.addAll(entityAssetDetailRecordList);
            assetDetailRecordAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onAssetDetailRecordFail(Exception e) {
        super.onAssetGTEDetailFail(e);
        if (assetDetailRecordAdapter != null) {
            assetDetailRecordAdapter.loadMoreFail();
        }
    }
}
